// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/Home.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'user',
          name: 'user',
          component: () => import('../views/user/index.vue'),
          meta: { requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'role',
          name: 'role',
          component: () => import('../views/role/index.vue'),
          meta: { requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'constant-category',
          name: 'constant-category',
          component: () => import('../views/constant-category/index.vue'),
          meta: { requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'constant-type',
          name: 'constant-type',
          component: () => import('../views/ConstantType.vue'),
          meta: { requiresAuth: true, requiresAdmin: true }
        },
        {
          path: 'registration',
          name: 'registration',
          component: () => import('../views/Registration.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: 'medical-record',
          name: 'medical-record',
          component: () => import('../views/MedicalRecord.vue'),
          meta: { requiresAuth: true, requiresDoctor: true }
        },
        {
          path: 'check-apply',
          name: 'check-apply',
          component: () => import('../views/CheckApply.vue'),
          meta: { requiresAuth: true, requiresDoctor: true }
        },
{
  path: 'perform-check',
  name: 'perform-check',
  component: () => import('../views/PerformCheck.vue'),
  meta: { 
    requiresAuth: true,
    requiresDoctor: true // 改为需要医生权限
  }
},
      {
  path: 'result-check',
  name: 'result-check',
  component: () => import('../views/resultcheck.vue'),
  meta: { 
    requiresAuth: true,
    requiresDoctor: true 
  }
},

      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue')
    }
  ]
});

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  const userType = localStorage.getItem('userType')
  const deptID = localStorage.getItem('deptID')

  // 检查是否需要登录
  if (to.meta.requiresAuth && !isAuthenticated) {
    ElMessage.error('请先登录')
    next('/login')
    return
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && userType !== '1') {
    ElMessage.error('只有管理员可以访问该页面')
    next(from.path || '/')
    return
  }

  // 检查是否需要医生权限
  if (to.meta.requiresDoctor && userType !== '3') {
    ElMessage.error('只有医生可以访问该页面')
    next(from.path || '/')
    return
  }

  // 检查是否需要检查科室权限
  if (to.meta.requiresCheckDepartment) {
    // 假设检查科室的deptID为5-10（根据实际业务调整）
    const checkDepts = ['5', '6', '7', '8', '9', '10']
    if (!checkDepts.includes(deptID)) {
      ElMessage.error('只有检查科室可以访问该页面')
      next(from.path || '/')
      return
    }
  }

  next()
})

export default router